package com.lanou.myim.ui.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMChatOptions;
import com.lanou.myim.R;
import com.lanou.myim.tools.SlideSwitch;

/**
 * Created by dllo on 16/3/9.
 */
public class SetActivity extends BaseActivity {
    private SlideSwitch inform;
    private SlideSwitch voice;
    private SlideSwitch shake;
    private SlideSwitch loudspeaker;
    private SharedPreferences first;
    private SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chatset);
        final View voiceButton = findViewById(R.id.newMsg_voice);
        final View shakeButton = findViewById(R.id.newMsg_shake);
        inform = (SlideSwitch) findViewById(R.id.slideSwitch_inform);
        voice = (SlideSwitch) findViewById(R.id.slideSwitch_voice);
        shake = (SlideSwitch) findViewById(R.id.slideSwitch_shake);
        loudspeaker = (SlideSwitch) findViewById(R.id.slideSwitch_loudspeaker);
        final EMChatOptions chatOptions = EMChatManager.getInstance().getChatOptions();

        first = getSharedPreferences("phoneSet", Context.MODE_PRIVATE);
        //获取上一次的状态
        if (first.getBoolean("setInform", true)) {
            inform.changeState();

            if (first.getBoolean("setVoice", true)) {
                voice.changeState();
            } else {
                chatOptions.setNoticeBySound(false);
            }
            if (first.getBoolean("setShake", true)) {
                shake.changeState();
            } else {
                chatOptions.setNoticedByVibrate(false);
            }
        } else {
            chatOptions.setNotifyBySoundAndVibrate(false);
            if (first.getBoolean("setVoice", true)) {
                voice.changeState();
            } else {
                chatOptions.setNoticeBySound(false);
            }
            if (first.getBoolean("setShake", true)) {
                shake.changeState();
            } else {
                chatOptions.setNoticedByVibrate(false);
            }
            voiceButton.setVisibility(View.GONE);
            shakeButton.setVisibility(View.GONE);
        }
        if (first.getBoolean("setSpeaker", true)) {
            loudspeaker.changeState();
        } else {
            chatOptions.setUseSpeaker(false);
        }

        editor = first.edit();

        //新消息通知
        inform.setOnChangeListener(new SlideSwitch.OnChangeListener() {
            @Override
            public void onChange(SlideSwitch sw, boolean state) {
                if (state) {
                    voiceButton.setVisibility(View.VISIBLE);
                    shakeButton.setVisibility(View.VISIBLE);
                } else {
                    voiceButton.setVisibility(View.GONE);
                    shakeButton.setVisibility(View.GONE);
                }
                Toast.makeText(SetActivity.this, "" + state, Toast.LENGTH_SHORT).show();
                chatOptions.setNotifyBySoundAndVibrate(state);
                editor.putBoolean("setInform", state);
                editor.commit();
            }
        });
        //声音
        voice.setOnChangeListener(new SlideSwitch.OnChangeListener() {
            @Override
            public void onChange(SlideSwitch sw, boolean state) {
                chatOptions.setNoticeBySound(state);
                editor.putBoolean("setVoice", state);
                editor.commit();
            }
        });
        //震动
        shake.setOnChangeListener(new SlideSwitch.OnChangeListener() {
            @Override
            public void onChange(SlideSwitch sw, boolean state) {
                chatOptions.setNoticedByVibrate(state);
                editor.putBoolean("setShake", state);
                editor.commit();
            }
        });
        ///是否使用扬声器
        loudspeaker.setOnChangeListener(new SlideSwitch.OnChangeListener() {
            @Override
            public void onChange(SlideSwitch sw, boolean state) {
                chatOptions.setUseSpeaker(state);
                editor.putBoolean("setSpeaker", state);
                editor.commit();
            }
        });
    }
}
